package com.funny.learning.english.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.funny.learning.english.GameBaseActivity;
import com.funny.learning.english.R;
import com.funny.learning.english.adapter.TuVungAdapter;
import com.funny.learning.english.adapter.TuVungAdapter.ClickListener;
import com.funny.learning.english.model.GameEntity;

public class TuVungActivity extends GameBaseActivity implements OnClickListener{
	private ViewPager pager; 
	
	private String TAG = "TuVungActivity"; 
	private TuVungAdapter adapter;
	
	private boolean isPause = false;

	private ImageView ivPause;

	private ImageView ivPlay;

	private ImageView ivRefresh;

	private ImageView ivBack;

	private LinearLayout llPause;

	private TextView tvTitle;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.tu_vung_layout);
		super.onCreate(savedInstanceState); 
		
//		intAB("Từ vựng");
		
		ivPause = (ImageView)findViewById(R.id.ivPause);
		ivPlay = (ImageView)findViewById(R.id.ivPlay);
		ivRefresh = (ImageView)findViewById(R.id.ivRefresh);
		ivBack = (ImageView)findViewById(R.id.ivBack);
		llPause = (LinearLayout)findViewById(R.id.llPause);
		tvTitle = (TextView)findViewById(R.id.tvTitle);
		
		ivBack.setOnClickListener(this);
		ivPause.setOnClickListener(this);
		ivPlay.setOnClickListener(this);
		ivRefresh.setOnClickListener(this);
		
	}
	
	@Override
	protected void initSucess() {
		// TODO Auto-generated method stub
		super.initSucess();
		
		setTitle("Từ vựng");
		
		Toast.makeText(activity, "size: " + mGameEntities.size(), 2000).show();
		
		pager = (ViewPager)findViewById(R.id.pager);
		
		adapter = new TuVungAdapter(activity, mFile, mGameEntities);
		pager.setAdapter(adapter);
		playMusic(getPathSound(mGameEntities.get(0)));
		
		pager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
//				String srcSound = arrGame.get(arg0).getLaudio();
				playMusic(getPathSound(mGameEntities.get(arg0)));
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
		
		adapter.setClickListener(new ClickListener() {
			
			@Override
			public void onPre() {
				int pos = pager.getCurrentItem() - 1;
				if(pos >= 0){
					pager.setCurrentItem(pos);
				}
			}
			
			@Override
			public void onNext() {
				int pos = pager.getCurrentItem() + 1;
				if(pos >= mGameEntities.size()){
					finishGame();
				}else{
					pager.setCurrentItem(pos);
				}
			}
			
			@Override
			public void getSound(GameEntity gameEntity) {
				playMusic(getPathSound(gameEntity));
			}
		});
	}
	 
	 
	
	@Override
	public void onBackPressed() {
		stopMusic();
		super.onBackPressed();
	}
	
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.ivPause:
			isPause = !isPause;
			if(isPause){
				ivPause.setImageResource(R.drawable.ic_play);
				llPause.setVisibility(View.VISIBLE);
			}else{
				ivPause.setImageResource(R.drawable.ic_pause);
				llPause.setVisibility(View.GONE);
			}
			break;
		case R.id.ivPlay:
			ivPause.setImageResource(R.drawable.ic_pause);
			llPause.setVisibility(View.GONE);
			break;
		case R.id.ivRefresh:
			ivPause.setImageResource(R.drawable.ic_pause);
			llPause.setVisibility(View.GONE);
			adapter = new TuVungAdapter(activity, mFile, mGameEntities);
			pager.setAdapter(adapter);
			playMusic(getPathSound(mGameEntities.get(0)));
			break;
		case R.id.ivBack:
			onBackPressed();
			break;

		default:
			break;
		}
	}
}
